
import yaml
import pytest

# 追加写入
def write_yaml(filename, data):
    with open(filename, encoding='utf-8', mode="a+") as f:
        yaml.dump(data, stream=f)

# 读取
def read_yaml(filename,key):
    with open(filename, encoding='utf-8', mode="r") as f:
        data = yaml.load(f,Loader=yaml.FullLoader)
        return data[key]

# 清空
def clear_yaml(filename):
    with open(filename, encoding='utf-8', mode="w") as f:
        f.truncate()

# yaml就是为了去除多余的引号，让文件格式更简单
def test_yaml():
    # 写入yaml文件
    data = {"str":"12345",
            "int":1234,
            "float":1234.5,}
    write_yaml("test.yml", data)

    # 读取yaml文件
    ret = read_yaml("test.yml","xdt")
    print(ret)

    # 清空yaml文件
    clear_yaml("test.yml")